package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.TemplateManager;
import Common.SERVER_COMMANDS;
//import Common.Entities.TEMPLATE_STATUS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * This class is a screen of adding a new marketing template
 * @author AL
 *
 */
public class TemplateAddScreen extends JPanel 
{
	
	private JLabel lblAddNewTemplate;
	private JLabel lblTempID;
	private JTextField textTempID;
	private JLabel lblTempName;
	private JTextField textTempName;
	private JLabel lblTempDesc;
	private JTextField textTempDesc;
	private JLabel lblProdID;
	private JTextField textProdID;
	private JLabel lblMarketingMessage;
	private JTextArea textAreaMsg;
	private JButton btnAddTemplate;
	private JButton btnCancelAction;

	

	/**
	 * Create the panel.
	 */
	public TemplateAddScreen() 
	{
	
		initialize();
	}
	private void initialize() 
	{
		setSize(450,372);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBackground(Color.WHITE);
		setLayout(null);
		
		add(getLblAddNewTemplate());
		
		add(getLblTempID());
		add(getTextTempID());
		
		add(getLblTempName());
		add(getTextTempName());
		
		add(getLblTempDesc());
		add(getTextTempDesc());
		
		add(getLblProdID());
		add(getTextProdID());
		
		add(getLblMsg());
		add(getTextAreaMsg());
		
		add(getBtnAddTemplate());
		add(getBtnCancelAction());
	}

		
	JLabel getLblAddNewTemplate()
	{
		if (lblAddNewTemplate == null) 
		{
			lblAddNewTemplate = new JLabel("Add Marketing Template");
			lblAddNewTemplate.setHorizontalAlignment(SwingConstants.CENTER);
			lblAddNewTemplate.setForeground(Color.RED);
			lblAddNewTemplate.setFont(new Font("David", Font.BOLD, 26));
			lblAddNewTemplate.setBackground(Color.WHITE);
			lblAddNewTemplate.setBounds(0, 0, 450, 49);
		}
		return lblAddNewTemplate;
	}
	
	
	JLabel getLblTempID() 
	{
		if (lblTempID == null) 
		{
	     lblTempID = new JLabel("Template ID:");
	     lblTempID.setFont(new Font("David", Font.BOLD, 20));
	     lblTempID.setBounds(10, 65, 130, 29);
		}
		return lblTempID;
	}
	
	
	JTextField getTextTempID() 
	{
		if (textTempID == null) 
		{
	     textTempID = new JTextField();
	     textTempID.setColumns(10);
	     textTempID.setBackground(SystemColor.menu);
	     textTempID.setBounds(171, 70, 251, 20);
		}
		return textTempID;
	}
	
	
	JLabel getLblTempName() 
	{
		if (lblTempName == null) 
		{
			lblTempName = new JLabel("Template Name:");
			lblTempName.setFont(new Font("David", Font.BOLD, 20));
			lblTempName.setBounds(10, 96, 164, 29);
		}
		return lblTempName;
	}
	
	
	JTextField getTextTempName() 
	{
		if (textTempName == null) 
		{
			textTempName = new JTextField();
			textTempName.setBackground(SystemColor.control);
			textTempName.setBounds(171, 101, 251, 20);
			textTempName.setColumns(10);
		}
		return textTempName;
	}
	

	
	JLabel getLblTempDesc() 
	{
		if (lblTempDesc == null) 
		{
			lblTempDesc = new JLabel("Description:");
			lblTempDesc.setFont(new Font("David", Font.BOLD, 20));
			lblTempDesc.setBounds(10, 128, 115, 29);
		}
		return lblTempDesc;
	}
	

	JTextField getTextTempDesc() 
	{
		if (textTempDesc == null) 
		{
	       textTempDesc = new JTextField();
	       textTempDesc.setColumns(10);
	       textTempDesc.setBackground(SystemColor.menu);
	       textTempDesc.setBounds(171, 132, 251, 20);
		}
		return textTempDesc;
	}
	
	JLabel getLblMsg() 
	{
		if ( lblMarketingMessage == null) 
		{
	      lblMarketingMessage = new JLabel("Message:");
	      lblMarketingMessage.setFont(new Font("David", Font.BOLD, 20));
	      lblMarketingMessage.setBounds(10, 199, 164, 29);
		}
		return  lblMarketingMessage;
	}
	
	
	JTextArea getTextAreaMsg() 
	{
		if (textAreaMsg == null) 
		{
			textAreaMsg = new JTextArea();
			textAreaMsg.setBorder(new LineBorder(new Color(0, 0, 0)));
			textAreaMsg.setBackground(SystemColor.control);
			textAreaMsg.setFont(new Font("Tahoma", Font.PLAIN, 11));
			textAreaMsg.setLineWrap(true);
			textAreaMsg.setBounds(171, 199, 251, 73);
		}
		return textAreaMsg;
	}
	
	
	
	private JLabel getLblProdID() 
	{
		if (lblProdID == null) 
		{
			lblProdID = new JLabel("Product ID:");
			lblProdID.setFont(new Font("David", Font.BOLD, 20));
			lblProdID.setBounds(10, 163, 115, 29);
		}
		return lblProdID;
	}
	
	private JTextField getTextProdID() 
	{
		if (textProdID == null) {
			textProdID = new JTextField();
			textProdID.setColumns(10);
			textProdID.setBackground(SystemColor.menu);
			textProdID.setBounds(171, 168, 251, 20);
		}
		return textProdID;
	}
		
		
	
	JButton getBtnAddTemplate() {
		if (btnAddTemplate == null) {
			btnAddTemplate = new JButton("Add");
			btnAddTemplate.addActionListener(new BtnSubmitNewTemplateActionListener());
			btnAddTemplate.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnAddTemplate.setBounds(152, 296, 83, 23);
		}
		return btnAddTemplate;
	}
	JButton getBtnCancelAction() {
		if (btnCancelAction == null) {
			btnCancelAction = new JButton("Cancel");
			btnCancelAction.addActionListener(new BtnCancelActionActionListener());
			btnCancelAction.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnCancelAction.setBounds(245, 296, 83, 23);
		}
		return btnCancelAction;
	}
	/**
	 * This inner class implements event listener for pressing Cancel Action button
	 * @author AL
	 *
	 */
	private class BtnCancelActionActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			textTempID.setText("");
			textTempName.setText("");
			textTempDesc.setText("");
			textProdID.setText("");
			textAreaMsg.setText("");
			TemplateManagementScreen tmScreen = new TemplateManagementScreen();
			setVisible(false);
			ClientStart.panelSwicher(tmScreen);
		}
	}
	/**
	 * This inner class implements event listener for pressing Add New Marketing Template
	 * @author AL
	 *
	 */
	private class BtnSubmitNewTemplateActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			String tempName;
			String tempID;
			String tempDesc;
			String ProdID;
			String tempMsg;
			try {
				tempName = textTempName.getText();
				tempID = textTempID.getText();
				tempDesc = textTempDesc.getText();
				ProdID = textProdID.getText();
				tempMsg = textAreaMsg.getText();
				if (tempID.isEmpty()||tempName.isEmpty()|| tempDesc.isEmpty()||tempMsg.isEmpty()||ProdID.isEmpty()) throw new NullPointerException();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_TEMPLATE, TemplateManager.addTemplate(tempID, tempName,tempDesc,tempMsg, "in building process",ProdID ));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				} 
			} catch (NullPointerException e) {
				JOptionPane.showMessageDialog(null, "All fields are mandatory", "Template was not added", JOptionPane.INFORMATION_MESSAGE);
			} 
		} 
	}
	
}
